package cn.ahaoweb.drule.core.compile.provide.hepler;

import cn.ahaoweb.drule.core.compile.provide.ObjectTree;
import cn.ahaoweb.drule.core.compile.structure.DruleBlock;
import cn.ahaoweb.drule.core.model.enums.NodeType;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ahao
 * @datetime 2025/6/4 13:59
 * @description 简单字段匹配器
 */
public class SimpleFieldMatcher implements FieldMatcher {


    /**
     * 当DruleBlock的When条件匹配时返回true，否则返回false。
     *
     * @param when      DruleBlock中的When条件
     * @param fieldNode 要匹配的字段节点
     * @return 如果匹配成功，则返回匹配的Condition列表；否则返回空列表
     */
    @Override
    public List<DruleBlock.Condition> matchWhen(DruleBlock.When when, ObjectTree.Node fieldNode) {
        List<DruleBlock.ConditionGroup> groups = when.getGroups();
        List<DruleBlock.Condition> matchedAonditions = new ArrayList<>();
        // 遍历条件列表进行匹配
        if (groups != null && groups.size() > 0) {
            for (DruleBlock.ConditionGroup group : groups) {
                if (group.getConditions() != null && group.getConditions().size() > 0) {
                    for (DruleBlock.Condition condition : group.getConditions()) {
                        // 匹配字段
                        DruleBlock.RuleFieldInfo fieldInfo = condition.getFieldInfo();
                        if (Objects.equals(fieldInfo.getFieldCode(), fieldNode.getFieldCode())
                                && Objects.equals(fieldInfo.getPath(), fieldNode.getPath())) {
                            // 如果字段编码和路径相同 则直接匹配成功
                            matchedAonditions.add(condition);
                        }
                        // 匹配值
                        if (Objects.equals(condition.getJudgeValue(), "${" + fieldNode.getPath() + "}")
                                || Objects.equals(condition.getJudgeValue(), "${" + fieldNode.getFieldCode() + "}")
                        ) {
                            // 如果判断值
                            matchedAonditions.add(condition);
                        }
                    }
                }
            }
        }
        return matchedAonditions;
    }

    /**
     * 根据给定的规则块和字段节点匹配并返回是否成功。
     *
     * @param then      规则块中的 Then 条件块
     * @param fieldNode 字段节点
     * @return 如果匹配成功，则返回匹配的Action列表；否则返回空列表
     */
    @Override
    public List<DruleBlock.Action> matchThen(DruleBlock.Then then, ObjectTree.Node fieldNode) {
        List<DruleBlock.Action> actions = then.getActions();
        List<DruleBlock.Action> matchedActions = new ArrayList<>();
        // 遍历结果列表进行匹配
        if (actions != null && actions.size() > 0) {
            for (DruleBlock.Action action : actions) {
                // 效验字段信息
                DruleBlock.RuleFieldInfo fieldInfo = action.getFieldInfo();
                if (Objects.equals(fieldInfo.getFieldCode(), fieldNode.getFieldCode())
                        && Objects.equals(fieldInfo.getPath(), fieldNode.getPath())) {
                    // 如果字段编码和路径相同 则直接匹配成功
                    matchedActions.add(action);
                }
                // 匹配值，待完善
            }
        }
        return matchedActions;
    }

}
